package com.heima.user.listens;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.heima.model.user.pojos.ApUser;
import com.heima.user.dao.DemoDAO;
import com.heima.user.service.ApUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class CommonListen<T> extends AnalysisEventListener<T> {

    private Class<T> tClass;
    List<T> list = new ArrayList<T>();

    public CommonListen(Class tClass) {
        this.tClass = tClass;
    }

    /**
     * 将
     * @param t
     * @param analysisContext
     */
    @Override
    public void invoke(T t, AnalysisContext analysisContext) {

        T newInstance = null;
        try {
            newInstance = tClass.newInstance();
            BeanUtils.copyProperties(t, newInstance);
            System.out.println("newInstance" + newInstance.toString());
            list.add(newInstance);
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        System.out.println("excel全部解析完毕后的收尾操作");
    }
    public List<T> getList() {
        return list;
    }
}
